//
//  LoginViewController.m
//  UI_NavigationCtrollerHomework
//
//  Created by dllo on 16/7/23.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "LoginViewController.h"
#import "SimpleLabel.h"
#import "SimpleTextField.h"
#import "SimpleButton.h"
#import "SimpleImageView.h"

@interface LoginViewController ()<UITextFieldDelegate>
@property (nonatomic, retain) SimpleTextField *tfOfUser;
@property (nonatomic, retain) SimpleTextField *tfOfPasswd;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
      self.view.backgroundColor = [UIColor whiteColor];
      UIButton *button = [[UIButton alloc]initWithFrame:CGRectMake(10, 10, 50, 50)];
      [button setTitle:@"返回" forState:UIControlStateNormal];
      [self.view addSubview:button];
      [button addTarget:self action:@selector(handlebutton:) forControlEvents:UIControlEventTouchUpInside];
      [button setTitleColor:[UIColor colorWithRed:0.431 green:0.969 blue:0.760 alpha:1.000] forState:UIControlStateNormal];
      [self createLabel];
      [self createTextField];
      [self createButton];
      [self createImageView];
}

- (void)handlebutton:(UIButton *)button {
      [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)createLabel {
      
      // 创建对象.
      SimpleLabel *labelOfUser = [[SimpleLabel alloc] initWithFrame:CGRectMake(20, 100, 100, 40)];
      [self.view addSubview:labelOfUser];
      
      
     
      
      // API:
      labelOfUser.text = @"用户名:";
      
      
      // 显示行数
      labelOfUser.numberOfLines = 0;
      
      // 对齐方式
      labelOfUser.textAlignment = NSTextAlignmentRight;
      
      
      // 密码Label
      SimpleLabel *labelOfPasswd = [SimpleLabel simpleLabelWithFrame:CGRectMake(20, 160, 100, 40) title:@"密码:"];
      [self.view addSubview:labelOfPasswd];
      
      
}

#pragma mark - 知识点2: UITextField
- (void)createTextField {
      
      // 创建对象
      self.tfOfUser = [[SimpleTextField alloc] initWithFrame:CGRectMake(140, 100, 200, 40)];
      [self.view addSubview:self.tfOfUser];
      
      
      // 常用API
      self.tfOfUser.placeholder = @"请输入用户名";
      
      // 边框风格
      self.tfOfUser.borderStyle = UITextBorderStyleRoundedRect;
      
      // 密文输入
      self.tfOfUser.secureTextEntry = YES;
      
      self.tfOfUser.returnKeyType = UIReturnKeyNext;
      
      self.tfOfUser.keyboardType = UIKeyboardTypeDefault;
      
      self.tfOfUser.keyboardAppearance = UIKeyboardAppearanceDark;
      
      // 指定delegate
      self.tfOfUser.delegate = self;
      
      // clearButton
      self.tfOfUser.clearButtonMode = UITextFieldViewModeAlways;
      
      
      // 密码.
      self.tfOfPasswd = [[SimpleTextField alloc] initWithFrame:CGRectMake(140, 160, 200, 40)];
      [self.view addSubview:self.tfOfPasswd];
      
      self.tfOfPasswd.placeholder = @"请输入密码";
      self.tfOfPasswd.borderStyle = 3;
      
      self.tfOfPasswd.delegate = self;
      
}

#pragma mark - TextField Delegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
      
      
      // 释放第一响应, 系统的实现就是回收键盘.
      [textField resignFirstResponder];
      
      return YES;
}


/** 点击 clear 按键时调用此方法. */
- (BOOL)textFieldShouldClear:(UITextField *)textField {
      
      return YES;
}

#pragma mark - 知识点3: UIButton
- (void)createButton {
      
      // 创建对象.
      SimpleButton *buttonOfLogin = [SimpleButton buttonWithType:UIButtonTypeSystem];
      [self.view addSubview:buttonOfLogin];
      
      
      buttonOfLogin.frame = CGRectMake(90, 300, 100, 40);
      
      [buttonOfLogin setTitle:@"登录" forState:UIControlStateNormal];
      
      [buttonOfLogin setTitleColor:[UIColor colorWithRed:0.431 green:0.969 blue:0.760 alpha:1.000] forState:UIControlStateNormal];
      
      // 添加点击事件.
      [buttonOfLogin addTarget:self action:@selector(handleLogin:) forControlEvents:UIControlEventTouchUpInside];
      
      
      SimpleButton *buttonOfCancel = [SimpleButton buttonWithType:UIButtonTypeSystem];
      [self.view addSubview:buttonOfCancel];
      
      
      buttonOfCancel.frame = CGRectMake(230, 300, 100, 40);
      
      [buttonOfCancel setTitleColor:[UIColor colorWithRed:0.431 green:0.969 blue:0.760 alpha:1.000] forState:UIControlStateNormal];
      
      [buttonOfCancel setTitle:@"注册" forState:UIControlStateNormal];
      
      [buttonOfCancel addTarget:self action:@selector(handleCancel:) forControlEvents:UIControlEventTouchUpInside];
      
}
- (void)handleLogin:(SimpleButton *)login {

      NSLog(@"登录成功");
}

- (void)handleCancel:(SimpleButton *)button {
      
      // 三个输入框清空.
      self.tfOfUser.text = @"";
      self.tfOfPasswd.text = @"";
     
}

#pragma mark - 知识点4: UIImageView
- (void)createImageView {
      
      // 创建对象
      	
      UIImage *image = [UIImage imageNamed:@"qq.jpg"];
      
      SimpleImageView *imageViewOfBackground = [[SimpleImageView alloc] initWithImage:image];
      
      [self.view addSubview:imageViewOfBackground];
       imageViewOfBackground.frame = self.view.bounds;
      [self.view sendSubviewToBack:imageViewOfBackground];
}


@end
